from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果
        self.camera.frame.save_state()
        self.play(FadeIn(dot1),FadeIn(dot2),FadeIn(dot3))
         #中心主题+框框标记
        text1 = Tex(r"\text{凑微分}",color=PINK
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,15,0])
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=23,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-30, 15, 0])

        self.play(Create(rectangle),Create(text1))

        #箭头引出
        arrow = Arrow(start=[-18,17,0],end=[-10,25,0], color=RED
                       , stroke_width=20, tip_length=1)
        #箭头引出
        arrow1 = Arrow(start=[-18,13,0],end=[-10,10,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow),Create(arrow1))

        #文本
        text2 = Tex(r"\text{直接凑微分(简单)}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([2,25,0])
        text4 = Tex(r"$\int \dfrac{\ln ^{2}x}{x}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,25,0])
        text3 = Tex(r"\text{构造凑微分(复杂)}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([2,10,0])
        text5 = Tex(r"$\int \dfrac{x+1}{x\left( 1+xe^{x}\right) }dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,10,0])
        self.play(Create(text2),Create(text3))
        self.play(Create(text4),Create(text5))

        #框框标记
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=11,height=8
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle.move_to([30,25,0])
        self.play(Create(rectangle))
        #例题1讲解
        text6 = Tex(r"$\int \dfrac{\ln ^{2}x}{x}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        text7 = Tex(r"$\int \ln ^{2}xd\ln x$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        text8 = Tex(r"$=\dfrac{1}{3}\ln ^{3}x+c$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-6,0])
        self.play(Write(text6))
        self.play(
            ReplacementTransform(text6,text7)
        )
        self.play(Write(text8))

        #例题1清空
        self.play(FadeOut(text7),FadeOut(text8))
        #框框移动
        self.play(rectangle.animate.stretch(2.0,dim=0).move_to([30,10,0]))

        #例题2讲解


        text9 = Tex(r"$\int \dfrac{x+1}{x\left( 1+xe^{x}\right) }dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        text10 = Tex(r"$\int \dfrac{\left( 1+x\right) e^{x}}{xe^{x}\left( 1+xe^{x}\right) }dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,0,0])
        text11 = Tex(r"$\int \dfrac{1}{xe^{x}\left( 1+xe^{x}\right) }dxe^{x}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,0,0])
        
        self.play(Write(text9))
        #箭头
        arrow2 = Arrow(start=[10,-7,0],end=[4,-4,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow2))
        text110 = Tex(r"$\left( xe^{x}\right) ^{'}=\left( x+1\right) e^{x}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-7,0])
        self.play(Write(text110))
        #框框快速闪动

        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=11,height=5
                            ,stroke_color=PURE_RED,stroke_width=20)
        rectangle1.move_to([30,-7,0])
        self.play(Create(rectangle1))
        self.play(FadeOut(rectangle1))

        self.play(ReplacementTransform(text9,text10),FadeOut(arrow2),FadeOut(text110))
        self.wait(0.1)
        self.play(ReplacementTransform(text10,text11))

        text1100 = Tex(r"$t=xe^{x}$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,0,0])
        


        text12 = Tex(r"$=\int \dfrac{1}{t\left( 1+t\right) }dt$",color=GREEN_A
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text12))
        text1100 = Tex(r"$t=xe^{x}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text12,RIGHT,buff=2)
        self.play(Write(text1100))
        text13 = Tex(r"$=\int \left( \dfrac{1}{t}-\dfrac{1}{t+1}\right) dt$",color=BLUE_A
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-8,0])
        self.play(Write(text13))

        text14 = Tex(r"$=\ln \left| \dfrac{t}{t+1}\right| +c$",color=YELLOW_A
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-16,0])
        self.play(Write(text14))
        text15 = Tex(r"$=\ln \left| \dfrac{xe^{x}}{1+xe^{x}}\right| +c$",color=RED_A
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-16,0])
        self.play(ReplacementTransform(text14,text15))
        self.wait()


